//
//  ProduceTemporarilyView.m
//  ZJControlsClass
//
//  Created by mac on 2024/11/29.
//

#import "ProduceTemporarilyView.h"
#import "ProduceLongTimeCollectionViewCell.h"


@interface ProduceTemporarilyView ()
//<UICollectionViewDelegate,UICollectionViewDataSource>


@property (nonatomic,strong) UIView * follewView;

@property (nonatomic,strong) UIView * iconView;
@property (nonatomic,strong) UIImageView * img;

@property (nonatomic,strong) UILabel * nameLabel;
@property (nonatomic,strong) UILabel * numLabel;
@property (nonatomic,strong) UILabel * companyLabel;
@property (nonatomic,strong) UILabel * timeLabel;
//@property (nonatomic,strong) UICollectionView * collectionView;
@property (nonatomic,strong) NSMutableArray * dataSource;

@property (nonatomic,strong) UIImageView * codeImg;
@property (nonatomic,strong) UILabel * msgLabel;

@property (nonatomic, strong) UIStackView *optionsStackView;  // 用于存放选项的容器

@end



@implementation ProduceTemporarilyView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self){
        
        self.backgroundColor = COLOR_RGB(50, 113, 237);

        [self configUI];
        
        
    }
    return self;
}


-(void)setParamer:(NSMutableDictionary *)paramer{
    _paramer = paramer;
    
    [self.img sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.paramer[@"photo"]]] placeholderImage:[UIImage imageNamed:@""]];
    self.nameLabel.text = [NSString stringWithFormat:@"姓名：%@",self.paramer[@"nickname"]];
    self.numLabel.text = [NSString stringWithFormat:@"编号：%@",self.paramer[@"idCode"]];
    self.companyLabel.text = [NSString stringWithFormat:@"单位：%@",self.paramer[@"unitName"]];
    self.timeLabel.text = [NSString stringWithFormat:@"期限：%@",self.paramer[@"expiryDate"]];

    [self.codeImg sd_setImageWithURL:[NSURL URLWithString:self.paramer[@"qrCode"]] completed:nil];
    
    self.dataSource = [NSMutableArray arrayWithArray:self.paramer[@"passageAreaList"]];
//    [self.collectionView reloadData];
    
    
    [self downTime];
    
    for (UIView *view in self.optionsStackView.arrangedSubviews) {
        [view removeFromSuperview];
    }
    
    // 添加新的选项
    for (NSDictionary *option in self.dataSource) {
        UIView *container = [[UIView alloc] init];
        container.backgroundColor = [UIColor whiteColor];
//        container.layer.borderWidth = 1;
        container.layer.cornerRadius = 2;
        container.layer.masksToBounds = YES;
//        container.layer.borderColor = [UIColor brownColor].CGColor;
        
        UILabel *label = [[UILabel alloc] init];
        label.text = option[@"code"];
        label.textColor = [UIColor colorWithHexString:@"#3271ED"];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:28 weight:UIFontWeightHeavy];
        [container addSubview:label];
        [self.optionsStackView addArrangedSubview:container];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(container);
        }];
        
        [container mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(28.5);
        }];
        
       
    }
}



-(void)configUI{
    
    [self addSubview:self.follewView];
    
    [self.follewView addSubview:self.iconView];
    [self.iconView addSubview:self.img];
    
    [self.follewView addSubview:self.nameLabel];
    [self.follewView addSubview:self.numLabel];
    [self.follewView addSubview:self.companyLabel];
    [self.follewView addSubview:self.timeLabel];
//    [self.follewView addSubview:self.collectionView];
    
    [self.follewView addSubview:self.codeImg];
    [self.follewView addSubview:self.msgLabel];
    
    
    self.optionsStackView = [[UIStackView alloc] init];
    self.optionsStackView.axis = UILayoutConstraintAxisHorizontal;
        self.optionsStackView.spacing = 3.5;
        self.optionsStackView.alignment = UIStackViewAlignmentCenter;
        self.optionsStackView.distribution = UIStackViewDistributionEqualSpacing;
        [self.follewView addSubview:self.optionsStackView];
    
    
    [self.follewView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.left.equalTo(self);
    }];
    
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.follewView.mas_centerX);
        make.top.equalTo(self.follewView).with.offset(ALIGN_HEIGHT_PT(30));
        make.size.equalTo(@(CGSizeMake(ALIGN_WIDTH_PT(150), ALIGN_WIDTH_PT(150))));
    }];
    
    [self.img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.iconView.mas_centerX);
        make.centerY.equalTo(self.iconView.mas_centerY);
        make.size.equalTo(@(CGSizeMake(ALIGN_WIDTH_PT(120), ALIGN_WIDTH_PT(120))));
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.follewView).with.offset(ALIGN_WIDTH_PT(106));
        make.top.equalTo(self.iconView.mas_bottom).with.offset(ALIGN_HEIGHT_PT(23));
    }];
    [self.numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel.mas_left);
        make.top.equalTo(self.nameLabel.mas_bottom).with.offset(ALIGN_HEIGHT_PT(10));
    }];
    
    [self.companyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel.mas_left);
        make.top.equalTo(self.numLabel.mas_bottom).with.offset(ALIGN_HEIGHT_PT(10));
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel.mas_left);
        make.top.equalTo(self.companyLabel.mas_bottom).with.offset(ALIGN_HEIGHT_PT(10));
    }];
    [self.optionsStackView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.equalTo(self.follewView.mas_right).offset(-29)
//        ;
//        make.left.equalTo(self.follewView.mas_left).offset(-29);
        make.centerX.equalTo(self.iconView);
        make.top.equalTo(self.timeLabel.mas_bottom).with.offset(ALIGN_HEIGHT_PT(10));
    }];
//    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self).with.offset(ALIGN_WIDTH_PT(73.5));
//        make.top.equalTo(self.timeLabel.mas_bottom).with.offset(ALIGN_HEIGHT_PT(10));
//        make.size.equalTo(@(CGSizeMake(SCREEN_WIDTH - ALIGN_WIDTH_PT(147), ALIGN_WIDTH_PT(33))));
//    }];
    
    [self.codeImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.follewView.mas_centerX);
        make.top.equalTo(self.optionsStackView.mas_bottom).with.offset(ALIGN_HEIGHT_PT(35));
        make.size.equalTo(@(CGSizeMake(ALIGN_WIDTH_PT(150), ALIGN_WIDTH_PT(150))));
    }];
    
    [self.msgLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.follewView.mas_centerX);
        make.top.equalTo(self.codeImg.mas_bottom).with.offset(ALIGN_HEIGHT_PT(10));
    }];
    
    
}

#pragma mark -- UICollectionViewDelegate,UICollectionViewDataSource
//-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
//    return 1;
//}
//-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
//    return self.dataSource.count;
//}
//-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
//
//        
//    ProduceLongTimeCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ProduceLongTimeCollectionViewCell" forIndexPath:indexPath];
//    cell.areaLabel.layer.borderColor = COLOR_RGB(50, 113, 237).CGColor;
//    cell.areaLabel.layer.borderWidth = 0;
//    cell.areaLabel.backgroundColor = [UIColor whiteColor];
//    cell.areaLabel.textColor = COLOR_RGB(50, 113, 237);
//    cell.areaLabel.text = self.dataSource[indexPath.row][@"code"];
//        
//    return cell;
//    
//}
//
//
//#pragma mark - item宽高
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
//
//    return CGSizeMake(ALIGN_WIDTH_PT(33), ALIGN_WIDTH_PT(33));
//    
//}

#pragma mark - <UICollectionViewDelegateFlowLayout>
//#pragma mark - X间距
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
//    return  ALIGN_WIDTH_PT(3.5);
//}
//#pragma mark - Y间距
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
//    return  ALIGN_WIDTH_PT(0);
//}

#pragma mark - - 倒计时
-(void)downTime{
    
        //__block 如果修饰指针时，指针相当于弱引用，指针对指向的对象不产生引用计数的影响
    __block int timeout = 60;
        //获取全局队列
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        //创建一个定时器 并将定时器的任务交给全局队列执行（串行 不会造成线程堵塞）
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
        //设置触发时间的间隔
    /*
     1.0 * NSEC_PER_SEC 间隔为1S
     0 * NSEC_PER_SEC 允许的误差为0S
     */
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
        //block内部 如果对当前对象的强引用属性修改 应该使用__weak typeof(self)weakSelf 修饰  避免循环调用
    __weak typeof(self)weakSelf = self;
        //设置定时器的触发事件
    dispatch_source_set_event_handler(timer, ^{
        timeout --;
            // 对时间进行判断
        if (timeout <= 0) {
                // 倒计时结束 验证码按钮打开用户交集
                // 关闭计时器
            dispatch_source_cancel(timer);
                //将按钮交给主线程
            dispatch_async(dispatch_get_main_queue(), ^{

                self->_block(@"");
                
            });
        }else{
                //处于正在倒计时，在主线程中刷新button上的title，时间-1秒
            dispatch_async(dispatch_get_main_queue(), ^{
                
                NSString * str = [NSString stringWithFormat:@"%d",timeout];
                NSMutableAttributedString * timeStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"二维码将在%@s秒后失效",str]];
                [timeStr addAttributes:@{NSForegroundColorAttributeName:COLOR_RGB(255, 181, 117)} range:NSMakeRange(5, str.length)];
                weakSelf.msgLabel.attributedText = timeStr;
                
            });
             
        }
        
    });
    dispatch_resume(timer);
    
}



#pragma mark -- 懒加载
-(UIView *)follewView{
    if(!_follewView){
        _follewView = [UIView new];
        _follewView.backgroundColor = COLOR_RGB(0, 103, 228);
    }
    return _follewView;
}


-(UIView *)iconView{
    if (!_iconView){
        _iconView = [UIView new];
        _iconView.layer.cornerRadius = ALIGN_WIDTH_PT(150) / 2;
        _iconView.layer.masksToBounds = YES;
        _iconView.layer.borderWidth = 0.5;
        _iconView.layer.borderColor = [UIColor whiteColor].CGColor;
        _iconView.backgroundColor = [UIColor clearColor];
    }
    return _iconView;
}

-(UIImageView *)img{
    if (!_img){
        _img = [UIImageView new];
        _img.layer.cornerRadius = ALIGN_WIDTH_PT(120) / 2;
        _img.layer.masksToBounds = YES;
    }
    return _img;
}

-(UILabel *)nameLabel{
    if (!_nameLabel){
        _nameLabel = [UILabel new];
        _nameLabel.textAlignment = NSTextAlignmentLeft;
        _nameLabel.textColor = [UIColor whiteColor];
        _nameLabel.font = FONT14;
    }
    return _nameLabel;
}

-(UILabel *)numLabel{
    if (!_numLabel){
        _numLabel = [UILabel new];
        _numLabel.textAlignment = NSTextAlignmentLeft;
        _numLabel.textColor = [UIColor whiteColor];
        _numLabel.font = FONT14;
    }
    return _numLabel;
}

-(UILabel *)companyLabel{
    if (!_companyLabel){
        _companyLabel = [UILabel new];
        _companyLabel.textAlignment = NSTextAlignmentLeft;
        _companyLabel.textColor = [UIColor whiteColor];
        _companyLabel.font = FONT14;
    }
    return _companyLabel;
}
-(UILabel *)timeLabel{
    if (!_timeLabel){
        _timeLabel = [UILabel new];
        _timeLabel.textAlignment = NSTextAlignmentLeft;
        _timeLabel.textColor = [UIColor whiteColor];
        _timeLabel.font = FONT14;
    }
    return _timeLabel;
}

//-(UICollectionView *)collectionView{
//    if (!_collectionView) {
//        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
//        layout.minimumLineSpacing = 0;
//        layout.minimumInteritemSpacing = 0;
//        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
//        _collectionView.delegate = self;
//        _collectionView.dataSource = self;
//        _collectionView.backgroundColor = [UIColor clearColor];
//        
//        //cell
//        [_collectionView registerClass:[ProduceLongTimeCollectionViewCell class] forCellWithReuseIdentifier:@"ProduceLongTimeCollectionViewCell"];
//
//    }
//    return _collectionView;
//}

-(UIImageView *)codeImg{
    if (!_codeImg){
        _codeImg = [UIImageView new];
        _codeImg.layer.cornerRadius = 10;
        _codeImg.layer.masksToBounds = YES;
        _codeImg.backgroundColor = [UIColor whiteColor];
    }
    return _codeImg;
}

-(UILabel *)msgLabel{
    if (!_msgLabel){
        _msgLabel = [UILabel new];
        _msgLabel.textAlignment = NSTextAlignmentLeft;
        _msgLabel.textColor = [UIColor whiteColor];
        _msgLabel.font = FONT14;
    }
    return _msgLabel;
}




@end
